//
//  XDWebInfoListViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-25.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDWebInfoListViewController.h"
#import "WebInfo_GetView_Task.h"
#import "InfoListModel.h"
#import "CommonListCell.h"
#import "DBFrameService.h"
#import "XDWebInfoDetailViewController.h"

@interface XDWebInfoListViewController ()
{
    NSString *categoryguid;
    NSString *WebInfo_GetView_Task_Guid;
    NSMutableArray *dataArray;
    int pageindex;
    int pagesize;
    NSString *keyword;
}
@end

@implementation XDWebInfoListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    categoryguid = [[intentParams objectForKey:@"categoryguid"]retain];
    NSString *isTzgg = [intentParams objectForKey:@"isTzgg"];
    if ([isTzgg isEqualToString:@"1"]) {
        categoryguid = [DBFrameService getTzggCategoryguid];
    }
    
    pageindex = 1;
    pagesize = 20;
    keyword = @"";
    dataArray = [[NSMutableArray alloc]init];
    [self getData];
    
//    self.tv.tableFooterView = [self getFooterViewButtonNormal];
}

-(UIView *)getFooterViewButtonNormal
{
    UIButton *lb = [[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]autorelease];
    [lb setTitle:@"加载更多..." forState:UIControlStateNormal];
    [lb setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [lb addTarget:self action:@selector(getData) forControlEvents:UIControlEventTouchUpInside];
    return lb;
}

-(UIView *)getFooterViewLoad
{
    UIActivityIndicatorView *pb = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [pb setFrame:CGRectMake(0, 0, 320, 50)];
    [pb startAnimating];
    return [pb autorelease];
}

-(void)getData
{
    self.tv.tableFooterView = [self getFooterViewLoad];
    NSMutableDictionary *params = [self getPassDictionary];
    [params setObject:[NSString stringWithFormat:@"%i",pageindex] forKey:@"CurrentPageIndex"];
    [params setObject:[NSString stringWithFormat:@"%i",pagesize] forKey:@"pagesize"];
    [params setObject:categoryguid forKey:@"categoryguid"];
    [params setObject:keyword forKey:@"keyword"];
    
    [self showMBPHUD];
    WebInfo_GetView_Task_Guid = [[[WebInfo_GetView_Task alloc]initWithContext:self andParams:params]startTask];
    pageindex++;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [self doSearch:searchBar];
}

- (void)doSearch:(UISearchBar *)searchBar{
    pageindex = 1;
    [dataArray removeAllObjects];
    keyword = [searchBar text];
    [self getData];
}

-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    if ([WebInfo_GetView_Task_Guid isEqualToString:threadGuid]) {
        
        NSMutableArray *dataArrayTemp = [[WebInfo_GetView_Task getInfoListModelArray:[NSString stringWithFormat:@"%@",obj]]retain];
        for (int i=0; i<dataArrayTemp.count; i++) {
            [dataArray addObject:[dataArrayTemp objectAtIndex:i]];
        }
        
        self.tv.tableFooterView = [self getFooterViewButtonNormal];
        if (dataArrayTemp.count<pagesize) {
            self.tv.tableFooterView = nil;
        }
        [dataArrayTemp release];
        [self.tv reloadData];
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        static NSString *WebinfolistItem = @"WebinfolistItem";
        CommonListCell *cell = (CommonListCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
        if (cell==nil) {
            NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"CommonListCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        InfoListModel *m = [dataArray objectAtIndex:indexPath.row];
        cell.lbTitle.text = m.txtTitle;
        
        cell.lbPerson.text = m.PostUserName;
        cell.lbDate.text = [[m.PostDate componentsSeparatedByString:@" "] objectAtIndex:0];
        if ([m.IsRead isEqualToString:@"1"]) {
            cell.ivStatus.hidden = YES;
            cell.lbTitle.frame = CGRectMake(7, cell.lbTitle.frame.origin.y, 307, cell.lbTitle.frame.size.height);
        }
        else
        {
            cell.ivStatus.hidden = NO;
        }
        return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    InfoListModel *m = [dataArray objectAtIndex:indexPath.row];
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:m.InfoGuid forKey:@"InfoGuid"];
    [params setObject:m.PostUserName forKey:@"PostUserName"];
    [params setObject:m.PostDate forKey:@"PostDate"];
    [params setObject:m.txtTitle forKey:@"txtTitle"];
    XDWebInfoDetailViewController *vc = [[XDWebInfoDetailViewController alloc]initWithParams:params];
    [self.navigationController pushViewController:vc animated:YES];
    [params release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tv release];
    [_searchBar release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setTv:nil];
    [self setSearchBar:nil];
    [super viewDidUnload];
}
@end
